/**
 * Created with nova.
 * Date: 14-7-11
 * Time: 上午1:09
 */

$(function(){
    var a = 0,
        b = 0;
    var c = 0;
    var d = $(window).height() >= $(window).width() ? $(window).height() : $(window).width();
    var e = $(window).height() <= $(window).width() ? $(window).height() : $(window).width();
    var f = 0,
        g = 0,
        h = [],
        i = 1,
        j = .7,
        k = 0,
        l = 5,
        m = -1; 
    var n = 1;
    var o = true;
    var p = 0;
    var q = false;
    var r = [{
        bb: [51, 71],
        score: [0, 10],
        src: appBase + "/Images/q1.png",
        txt: ["15年","20年"],
        size: 15,
        bsize: 14
    }, {
        bb: [46, 66],
        score: [10, 0],
        src: appBase + "/Images/q2.png",
        txt: ["i战略","A战略"],
        size: 15,
        bsize: 14
    }, {
        bb: [50, 70],
        score: [10, 0],
        src: appBase + "/Images/q3.png",
        txt: ["掌上四川","成都范儿"],
        size: 15,
        bsize: 14
    }, {
        bb: [46, 66],
        score: [10, 0],
        src: appBase + "/Images/q4.png",
        txt: ["成都范儿和成都红娘","成都红娘和华西都市报"],
        size: 15,
        bsize: 14
    }, {
        bb: [51, 71],
        score: [10, 0],
        src: appBase + "/Images/q5.png",
        txt: ["爱哟","广场舞"],
        size: 15,
        bsize: 14
    }];
    var s = [{
        title: "华西20年有奖问答",
        content: "华西20年有奖问答"
    }, {
        title: "华西20年有奖问答",
        content: "华西20年有奖问答"
    }, {
        title: "华西20年有奖问答",
        content: "华西20年有奖问答"
    }, {
        title: "华西20年有奖问答",
        content: "华西20年有奖问答"
    }, {
        title: "华西20年有奖问答",
        content: "华西20年有奖问答"
    }];
    var t = [
        ["cl4.png", "cl1.png", "cl2.png", "cl3.png", "cl4.png", "cl1.png"],
        ["cl1.png", "cl4.png", "cl2.png", "cl3.png", "cl1.png", "cl4.png"],
        ["cl1.png", "cl4.png", "cl2.png", "cl3.png", "cl1.png", "cl4.png"],
        ["cl4.png", "cl2.png", "cl1.png", "cl3.png", "cl4.png", "cl2.png"],
        ["cl4.png", "cl3.png", "cl2.png", "cl1.png", "cl4.png", "cl3.png"]
    ];
    var u = 0;
    var v = document.getElementById('finishsnd');
    var w = document.getElementById('startsnd');
    var _hmt = [];
    TweenMax.set($(".s1btl"), {
        alpha: .3
    });
    $("#main")[0].addEventListener(touchEvents.touchstart, J, false);
    $("#main")[0].addEventListener(touchEvents.touchmove, K, false);
    
    $("#lockDiv")[0].addEventListener(touchEvents.touchmove, function(O) {
        O.preventDefault()
    }, false);
    $("#lockDiv")[0].addEventListener(touchEvents.touchstart, function(O) {
        O.preventDefault()
    }, false);
    p = window.devicePixelRatio;
    if (p >= 1.5 && $(window).width() <= 320 && $(window).height() <= 460 && browser.versions.android) {
        q = true
    };
    $("#sharebbt").click(function() {
        location.href = "http://weilink.huaxi100.com/spe/hx20";
    });
    $("#shareBt2").click(function() {
        o = false;
        //y();
        _hmt.push(['_trackEvent', "其他", "clicktoshare"]);
        if (isLast && $("#shareR").css("display") != "none") {
            TweenMax.to($('#shareR'), 1, {
                rotationY: -120,
                alpha: 0,
                transformPerspective: 600,
                transformOrigin: "center center",
                onComplete: function() {
                    $('#shareR').hide()
                },
                ease: Quart.easeInOut
            })
        }
    });
    $("#loadingObj").hide();
    TweenMax.set($(".s2qbg-color"), {
        alpha: .8
    });
    N();
    TweenMax.set($('#s2q'), {
        rotationY: 120,
        alpha: 0,
        transformPerspective: 600,
        transformOrigin: "center center"
    });
    TweenMax.set($(".shareRbg-color"), {
        alpha: .8
    });
    TweenMax.set($('#shareR'), {
        css: {
            "width": $(window).width() * .8,
            "left": $(window).width() / 2 - ($(window).width() * .8) / 2,
            "height": $(window).width() * .8 / (517 / 612)
        }
    });
    $('#s2q').hide();
    $('#ss1').show();
    for (var x = 1; x <= 4; x++) {
        $("#q" + x)[0].addEventListener(touchEvents.touchend, z)
    };

    function y() {
        $("#tips").show();
        TweenMax.set($("#tips"), {
            alpha: 0,
            y: -10
        });
        TweenMax.to($("#tips"), .5, {
            alpha: 1,
            y: 0,
            onComplete: function() {
                $("#tips")[0].addEventListener(touchEvents.touchend, function() {
                    TweenMax.to($("#tips"), .5, {
                        alpha: 0,
                        y: -10,
                        onComplete: function() {
                            $("#tips").hide()
                        }
                    })
                })
            },
            ease: Quart.easeInOut
        })
    };

    function z(O) {
        var P = O.currentTarget.id;
        var Q = P.split("q")[1];
    	if(r[c - 1].score[Q - 1]<10){
    		alert("哎呀,答错了哦！试试其他答案");
    		return false;
    	}
        for (var R = 1; R <= r[c - 1].bb.length; R++) {
            $("#q" + R)[0].removeEventListener(touchEvents.touchend, z)
        };
        u += r[c - 1].score[Q - 1];
        c++;
        TweenMax.to($('#q' + Q).children("div"), .2, {
            css: {
                color: "#c69c6d"
            },
            onComplete: function() {
                w.play();
                TweenMax.to($('#s2q'), .5, {
                    rotationY: -120,
                    alpha: 0,
                    transformPerspective: 600,
                    transformOrigin: "center center",
                    onComplete: function() {
                        if (c < 6) {
                            for (var S = 1; S < 5; S++) {
                                $('#q' + Q).children("div").css({
                                    "color": "#ffffff"
                                })
                            };
                            TweenMax.set($('#s2q'), {
                                rotationY: 120,
                                alpha: 0,
                                transformPerspective: 600,
                                transformOrigin: "center center",
                                onComplete: function() {
                                    $('#s2q').hide();
                                    F();
                                    $('.s2qbg').css({
                                        "background-image": "url(" + r[c - 1].src + ")"
                                    })
                                }
                            });
                            TweenMax.to($('#ss' + (c - 1)), .3, {
                                y: "2",
                                scale: 1 + (c - 1) * .01,
                                alpha: 0,
                                transformOrigin: "center top"
                            });
                            TweenMax.to($('#qbg'), .5, {
                                scale: 1 + (c - 1) * .05,
                                transformOrigin: "center 20%"
                            });
                            setTimeout(G, 1300);
                            setTimeout(function() {
                                $('#ss' + c).show();
                                TweenMax.set($('#ss' + c), {
                                    alpha: 0
                                });
                                TweenMax.to($('#ss' + c), .5, {
                                    alpha: 1
                                })
                            }, 200)
                        }
                    },
                    ease: Quart.easeInOut
                })
            }
        });
        if (c == 6) {
            TweenMax.set($("#rbb"), {
                alpha: 1
            });
            TweenMax.set($('#resBoard'), {
                alpha: 0
            });
    		//结果提示页。
            /*if (u >= 0 && u <= 20) n = 1;
            else if (u >= 21 && u <= 40) n = 2;
            else if (u >= 41 && u <= 60) n = 3;
            else if (u >= 61 && u <= 80) n = 4;
            else if (u >= 81 && u <= 100) n = 5;*/
    		n = 1;
            //$("#resBoard").css({
                //"background": "url(../Images/res" + n + ".png) center 6% no-repeat",
                //"background-size": "100% auto"
            //});
            $(".rxtxt").css({
                "background": "#000818 url(" + appBase + "/Images/r" + n + ".png) center 40% no-repeat",
                "background-size": "100% auto"
            });
            $("#rbb").hide();
            TweenMax.set($('.rxtxt'), {
                alpha: 0,
                scale: 1.2
            });
            $("#result").show();
            TweenMax.set($('.resultBgl'), {
                x: -e
            });
            TweenMax.to($(".resultBgl"), 1, {
                delay: 1.5,
                x: 0,
                ease: Quart.easeInOut
            });
            TweenMax.set($('.resultBgr'), {
                x: e
            });
            TweenMax.to($(".resultBgr"), 1, {
                delay: 1.5,
                x: 0,
                ease: Quart.easeInOut
            });
            TweenMax.to($('.rxtxt'), .5, {
                delay: 2.5,
                alpha: 1,
                scale: 1,
                onComplete: function() {
                    $("#rbb").show();
                    TweenMax.set($('#rbb'), {
                        alpha: 0
                    });
                    TweenMax.to($('#rbb'), 1, {
                        delay: .5,
                        alpha: 1
                    });
                    $("#mainPage").hide();
                    $("#main")[0].addEventListener(touchEvents.touchmove, A, false)
                },
                ease: Bounce.easeOut
            });
            setTimeout(function() {
                v.play()
            }, 1400)
        }
    };

    function A(O) {
        TweenMax.to($('.rxtxt'), 1.5, {
            css: {
                "height": 0
            },
            onComplete: function() {
                $("#resBoard").show();
                // descContent = s[n - 1].content;
                // shareTitle = s[n - 1].title;
                descContent = '华西20年有奖问答';
                shareTitle = '华西20年有奖问答';
                lineLink = 'http://weilink.huaxi100.com/spe/hx20';
                if (o) setTimeout(y, 5000);
                for (var P = -1; P < 5; P++) {
                    $("#rpic" + (P + 1)).css({
                        'background': 'url(' + appBase + '/Images/' + t[n - 1][P + 1] + ') left 20% no-repeat',
                        'backgroundPosition': ($(window).width() / 2 - $('#rpic1').width() * j / 2) + $(window).width() * (P - k) * i + 'px 35%',
                        "background-size": "70% auto"
                    })
                };
                TweenMax.to($("#resBoard"), .5, {
                    alpha: 1,
                    onComplete: function() {
                        // $("#rpic5")[0].addEventListener(touchEvents.touchstart, C, false);
                        // $("#rpic5")[0].addEventListener(touchEvents.touchmove, D, false);
                        // $("#rpic5")[0].addEventListener(touchEvents.touchend, E, false);
                        // isLast = true
                    }
                })
            },
            ease: Quart.easeInOut
        });
        TweenMax.to($("#rbb"), .5, {
            y: "10",
            alpha: 0,
            onComplete: function() {
                $("#rbb").hide()
            },
            ease: Quart.easeInOut
        });
        $("#main")[0].removeEventListener(touchEvents.touchmove, A, false)
    };

    function B() {
        for (var O = 0; O < 4; O++) {
            h[O] = parseInt($("#rpic" + (O + 1)).css('backgroundPosition'))
        }
    };

    function C(O) {
        if ($("#shareR").css("display") == "none") {
            TweenMax.killAll();
            var P = O.touches[0];
            g = P.pageX;
            B()
        }
    };

    function D(O) {
        O.preventDefault();
        if ($("#shareR").css("display") == "none") {
            TweenMax.killAll();
            var P = O.touches[0];
            f = P.pageX
        }
    };

    function E(O) {
        if (isLast && $("#shareR").css("display") != "none") {
            TweenMax.to($('#shareR'), 1, {
                rotationY: -120,
                alpha: 0,
                transformPerspective: 600,
                transformOrigin: "center center",
                onComplete: function() {
                    $('#shareR').hide()
                },
                ease: Quart.easeInOut
            })
        } else {
            if (f - g < 0) {
                if (k < (l - 1)) k++;
                _hmt.push(['_trackEvent', "其他", "leftarrow"])
            } else {
                if (k > m) k--;
                _hmt.push(['_trackEvent', "其他", "rightarrow"])
            };
            for (var P = -1; P < 5; P++) {
                TweenMax.to($("#rpic" + (P + 1)), .5, {
                    css: {
                        'backgroundPosition': ($(window).width() / 2 - $('#rpic1').width() * j / 2) + $(window).width() * (P - k) * i + 'px 35%',
                        "background-size": "70% auto"
                    },
                    ease: Quart.easeInOut
                })
            };
            setTimeout(function() {
                if (k == l - 1) {
                    k = 0;
                    for (var Q = -1; Q < 5; Q++) {
                        TweenMax.set($("#rpic" + (Q + 1)), {
                            css: {
                                'backgroundPosition': ($(window).width() / 2 - $('#rpic1').width() * j / 2) + $(window).width() * (Q - k) * i + 'px 35%',
                                "background-size": "70% auto"
                            }
                        })
                    }
                };
                if (k == m) {
                    k = 3;
                    for (var Q = -1; Q < 5; Q++) {
                        TweenMax.set($("#rpic" + (Q + 1)), {
                            css: {
                                'backgroundPosition': ($(window).width() / 2 - $('#rpic1').width() * j / 2) + $(window).width() * (Q - k) * i + 'px 35%',
                                "background-size": "70% auto"
                            }
                        })
                    }
                }
            }, 500)
        }
    };

    function F() {
        for (var O = 1; O <= 5; O++) {
            $("#q" + O).hide()
        };
        for (var P = 0; P < r[c - 1].bb.length; P++) {
            $("#q" + (P + 1)).show();
            $("#q" + (P + 1)).css({
                "top": r[c - 1].bb[P] + "%",
                "background": "url(" + appBase + "/Images/sel" + P + ".png) left 2px no-repeat",
                "background-size": "auto " + q ? r[c - 1].bsize - 4 : r[c - 1].bsize + "px"
            });
            $("#q" + (P + 1)).children("div").text(r[c - 1].txt[P]);
            $("#q" + (P + 1)).children("div").css({
                'font-size': q ? r[c - 1].size - 5 : r[c - 1].size
            })
        };
        for (var P = 1; P <= r[c - 1].bb.length; P++) {
            $("#q" + P)[0].addEventListener(touchEvents.touchend, z)
        }
    };

    function G() {
        $('#s2q').show();
        TweenMax.to($('#s2q'), .5, {
            rotationY: 0,
            alpha: 1,
            transformPerspective: 600,
            transformOrigin: "center center",
            ease: Quart.easeInOut
        })
    };

    function H(O) {
        TweenMax.to(O, 1.5, {
            alpha: 0,
            onComplete: I,
            onCompleteParams: [O],
            ease: Quart.easeInOut
        })
    };

    function I(O) {
        TweenMax.to(O, 1.5, {
            alpha: 1,
            onComplete: H,
            onCompleteParams: [O],
            ease: Quart.easeInOut
        })
    };

    function J(O) {
        var P = O.touches[0];
        b = P.pageY
    };

    function K(O) {
        O.preventDefault();
        var P = O.touches[0];
        a = P.pageY;
        if (c == 0) {
            L()
        }
    };

    function L() {
    	alert('L');
        c = 1;
        w.play();
        TweenMax.to($("#s1txt"), 1, {
            y: -$("#s1txt").height(),
            ease: Quart.easeInOut
        });
       
        TweenMax.to($("#s1bb"), .5, {
            y: "10",
            alpha: 0,
            ease: Quart.easeInOut
        });
        TweenMax.to($("#s1Bg"), .3, {
            delay: .8,
            alpha: 0,
            onComplete: function() {
                $("#s1").hide();
                H($(".sbg1"))
            }
        });
        setTimeout(M, 1500);
        _hmt.push(['_trackEvent', "其他", "entergame"]);
        _hmt.push(['_trackPageview', "/entergame.html"])
    };
    
    
    //
    function HD() {
    	alert('HD');
    	TweenMax.to($("#hdgz"), 1, {
            y: -$("#hdgz").height(),
            ease: Quart.easeInOut
        });
    	
    }
    //
    
    
    

    function M() {
        F();
        $('#s2q').show();
        TweenMax.to($('#s2q'), .8, {
            rotationY: 0,
            alpha: 1,
            transformPerspective: 600,
            transformOrigin: "center center",
            ease: Quart.easeInOut
        })
    };
    // window.addEventListener("resize", N, true);

    function N() {
        if ($(window).width() <= $(window).height()) {
            d = $(window).height() >= $(window).width() ? $(window).height() : $(window).width();
            e = $(window).height() <= $(window).width() ? $(window).height() : $(window).width();
            $("#lockDiv").hide();
            TweenMax.set($("#s2q"), {
                css: {
                    "width": $(window).width() * .8 + "px"
                }
            });
            TweenMax.set($("#s2q"), {
                css: {
                    "height": $(window).width() * .8 / (517 / 596) + "px"
                }
            });
            TweenMax.set($("#s2q"), {
                x: $(window).width() / 2 - $(window).width() * .8 / 2,
                y: d * .10
            })
        } else {
            d = $(window).height() >= $(window).width() ? $(window).height() : $(window).width();
            e = $(window).height() <= $(window).width() ? $(window).height() : $(window).width();
            $("#lockDiv").show()
        }
    }

});


